package 树.middle;

import 树.easy.ErSanWu;

/**
 * @author Lw中
 * @date 2021/7/26 23:54
 */
public class ErSanLu {

    // 236.二叉树的最近公共祖先
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || p == root || q == root) return root;
        /**
         * LWZ TODO : 2021/7/26 递归语句理解模糊不清
         *
         */
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        return left == null ? right : (right == null ? left : root);
    }

}
